//
//  OneRootModel.m
//  lTTheOne
//
//  Created by AA on 2024/3/11.
//

#import "OneRootModel.h"
#import <objc/runtime.h>
@implementation OneRootModel

#pragma mark - 字典转模型
+ (instancetype)initDictForModel:(NSDictionary *)dict{
    id object = [[self alloc] init];
    
    [object setValuesForKeysWithDictionary:dict];

    return object;
}

#pragma mark - 模型转字典
- (NSDictionary*)modelForDict{
    
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    //属性个数变量
    unsigned int propertyCount;
    //获取模型属性字段
    objc_property_t  *property = class_copyPropertyList([self class], &propertyCount);
    
    for (int index = 0; index < propertyCount; index ++) {
        
        objc_property_t  prop = property[index];
        //属性名称
        NSString *propertyName = [NSString stringWithUTF8String:property_getName(prop)];
        //属性值
        id propertyValue = [self valueForKey:propertyName];
        
        if (propertyValue == nil) {
            propertyValue = @"";
        }else{
            propertyValue = [self getObjectInternal:propertyValue];
        }
        [dict setObject:propertyValue forKey:propertyName];
    }
    
    if (property) {
        free(property);
    }
    
    return dict;
}

- (id)getObjectInternal:(id)obj
{
    
    if ([obj isKindOfClass:[NSNull class]]) {
        return @"";
    }
    
    if([obj isKindOfClass:[NSString class]]
       || [obj isKindOfClass:[NSNumber class]]
       ){
        return obj;
    }
    
    
    if([obj isKindOfClass:[NSArray class]]){
        NSArray *objarr = obj;
        NSMutableArray *arr = [NSMutableArray arrayWithCapacity:objarr.count];
        for(int i = 0;i < objarr.count; i++){
            [arr setObject:[self getObjectInternal:[objarr objectAtIndex:i]] atIndexedSubscript:i];
            
        }
        return arr;
    }
    
    if([obj isKindOfClass:[NSDictionary class]]){
        NSDictionary *objdic = obj;
        NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithCapacity:[objdic count]];
        for(NSString *key in objdic.allKeys){
            [dic setObject:[self getObjectInternal:[objdic objectForKey:key]] forKey:key];
        }
        
        return dic;
        
    }
    return [self modelForDict];
}

//指定类型转字典
- (NSDictionary*)modelForDict:(id)model{
    
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    
    //属性个数变量
    unsigned int propertyCount;
    //获取模型属性字段
    objc_property_t  *property = class_copyPropertyList([model class], &propertyCount);
    
    for (int index = 0; index < propertyCount; index ++) {
    
        objc_property_t  prop = property[index];
        //属性名称
        NSString *propertyName = [NSString stringWithUTF8String:property_getName(prop)];
        //属性值
        id propertyValue = [model valueForKey:propertyName];
        
        if (propertyValue == nil) {
            propertyValue = @"";
        }else{
            propertyValue = [self getObjectInternal:propertyValue];
        }
        [dict setObject:propertyValue forKey:propertyName];
    }
    
    if (property) {
        free(property);
    }
    
    return dict;
}


//字典转字符串
+ (NSString *)jsonStringFromDictionary:(NSDictionary *)dictionary
{
    if (!dictionary) {
        return nil;
    }
    else {
        NSData * jsonData = [NSJSONSerialization dataWithJSONObject:dictionary options:NSJSONWritingPrettyPrinted error:nil];
        NSString * jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        return jsonString;
    }
}

//字符串转字典
+ (NSDictionary *)dictionaryFromJSONString:(NSString *)jsonString
{
    if (!jsonString) {
        return nil;
    }
    else {
        NSData * jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
        NSDictionary * dict = [NSJSONSerialization JSONObjectWithData:jsonData options:NSJSONReadingAllowFragments | NSJSONReadingMutableContainers error:nil];
        return dict;
    }
}




@end
